package com.lagou.server;

import com.lagou.model.TestPaper;
import com.lagou.model.TestScore;
import com.lagou.model.User;
import com.lagou.model.Message;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 编程实现服务器的功能
 */
public class ServerView {

    /**
     * 使用合成复用原则
     */
    private ServerInitClose sic;

    private ServerUserDao sd ;

    private ServerTestPaperDao stpd;
    public ServerView(ServerInitClose sic, ServerUserDao sd,ServerTestPaperDao stpd){
        this.sic = sic;
        this.sd = sd;
        this.stpd = stpd;

    }



    /**
     * 自定义成员方法实现客户端发来的消息并接受处理
     */
    public void serverReceive() throws IOException, ClassNotFoundException {
        while (true){
                Message tum = (Message) sic.getOis().readObject();
                System.out.println("接收到的消息是:"+tum);
                switch (tum.getType()){
                    //管理员验证
                    case "managerCheck" :
                        if (sd.ServerManagerCheck(tum.getUser())){
                            tum.setType("success");
                        }else {
                            tum.setType("fail");
                        }
                        break;
                        //普通用户验证
                    case "userCheck" :
                        //调用方法实现学员账号和密码信息的校验
                        if (sd.ServerUserCheck(tum.getUser())){
                            tum.setType("success");
                        }else {
                            tum.setType("fail");
                        }
                        break;
                    case "SaveStudent" :
                        //调用新增学员方法
                        if (sd.ServerInsertStudent(tum.getUser())){
                            tum.setType("success");
                        }else {
                            tum.setType("fail");
                        }
                        break;
                    case "selectStudentByStudentId" :
                        //调用根据学号查找学员方法
                        User user = sd.ServerSelectUserByStudentId(tum.getUser().getStudentId());
                        if (user!=null){
                            tum.setType("success");
                            tum.setUser(user);
                        }else {
                            tum.setType("fail");
                        }
                        break;
                    case "selectAllStudent" :
                        //调用查找全部学员方法
                        List<User> users = sd.ServerSelectAllUser();
                        if (users!=null){
                            tum.setType("success");
                            tum.setUserList(users);
                            sic.getOos().reset();
                        }else {
                            tum.setType("fail");
                        }
                        break;
                    case "updateStudent" :
                        //调用修改学员方法
                        if (sd.ServerUpdateUserByStudentId(tum.getUser().getStudentId(),tum.getUser().getUser())){
                            tum.setType("success");
                        }else {
                            tum.setType("fail");
                        }
                        break;
                    case "removeStudent" :
                        //调用删除学员方法
                        if (sd.ServerDeleteUserByStudentId(tum.getUser().getStudentId())){
                            tum.setType("success");
                        }else {
                            tum.setType("fail");
                        }
                        break;
                        //退出系统
                    case "Quit" :
                        sd.closeSavaData();
                        stpd.closeSavaData();
                        return;
                        //保存考题
                    case "saveExamQuestions":
                        if (stpd.ServerInsertTestPaper(tum.getPaper())) {
                            tum.setType("success");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                        //删除考题
                    case "removeExamQuestions":
                        if (stpd.ServerRemoveTestPaper(tum.getPaper().getTopic())) {
                            tum.setType("success");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                        //修改考题
                    case "updateExamQuestions":
                        if (stpd.ServerUpdateTestPaper(tum.getPaper().getTopic(),tum.getPaper().getTestPaper())) {
                            tum.setType("success");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                        //根具题目查找考题
                    case "selectExamQuestionsByTopic":
                        TestPaper testPaper = stpd.ServerSelectTestPaperByTopic(tum.getPaper().getTopic());
                        if (testPaper!=null) {
                            tum.setType("success");
                            tum.setPaper(testPaper);
                        } else {
                            tum.setType("fail");
                        }
                        break;
                        //查询全部考题
                    case "selectAllExamQuestions":
                        List<TestPaper> testPapers = stpd.ServerSelectAllTestPaper();
                        if (testPapers!=null) {
                            tum.setType("success");
                            tum.setListTestPaper(testPapers);
                            sic.getOos().reset();
                        } else {
                            tum.setType("fail");
                        }
                        break;
                        //导入考题
                    case "importExamQuestions":
                        if (  stpd.ServerImportTestPager(tum.getPath())) {
                            tum.setType("success");
                        } else {
                            tum.setType("fail");
                        }
                        break;
                    case "userPasswordUpdate" :
                        //调用方法实现学员账号和密码信息的校验
                        if (sd.ServerUpdateUserPassword(tum.getUser().getStudentId(),tum.getUser().getUser())){
                            tum.setType("success");
                        }else {
                            tum.setType("fail");
                        }
                        break;
                    case "testAnswerCheck" :
                        //调用方法实现试卷答案单位校验
                        stpd.ServerTestAnswerCheck(tum.getTestScore());
                        tum.setType("success");
                        break;
                    case "resultSelect" :
                        //调用方法实现学员成绩查询
                        TestScore testScore = stpd.ServerTestAnswerSelect(tum.getTestScore());
                        if (null!=testScore){
                            tum.setType("success");
                            tum.setTestScore(testScore);
                        }else {
                            tum.setType("fail");
                        }
                        break;
                    case "exportResult" :
                        //调用方法实现学员成绩导出
                        System.out.println();
                        boolean boo = stpd.ServerExportResultByStudentId(tum.getUser().getStudentId(), tum.getPath());
                        if (boo){
                            tum.setType("success");
                        }else {
                            tum.setType("fail");
                        }
                        break;

                }
                //将校验结果发送给客户端
                sic.getOos().writeObject(tum);
                System.out.println("服务器发送校验结果成功!");
            }

        }
    }


